package auctionhaus

import static org.junit.Assert.*

import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit

import grails.converters.JSON
import grails.test.GrailsUnitTestCase
import com.icegreen.greenmail.util.*
import org.junit.*

class ListingIntegrationTests {

	def listingCompletePostMessageJob
	def listingCompleteReminderJob
	def grailsApplication
	def jmsService
	def listeningService
	def completedListingService
	def emailService

	def mailService
	def greenMail

	@Before
	public void setUp() {
	}

	@After
	public void tearDown() {

		greenMail.deleteAllMessages()
	}

	//C-1: When a listing completes post a message to a JMS Queue representing sending the completed listing information
	//including listing details, seller and winner information as XML or JSON (you choose). Test this with an integration test.
	@Test
	void testJMSMessageReceived()
	{
		Customer seller = new Customer(username:'steve@email.com',password:"thispass")
		seller.save(flush:true)

		Customer winner = new Customer(username:'bob@email.com',password:"thispas2")
		winner.save(flush:true)

		Listing listing = new Listing(name:"Dodge Stratus", endDateTime: new Date()-1, isFinished:true, startingPrice: 10, seller: seller, winner:winner)
		listing.save(flush:true)

		JSON.use("deep")
		def converter = listing as JSON
		def sentMessage = converter.toString()
		assertNotNull(sentMessage)

		def messagesBefore = jmsService.browse('queue')
		assertFalse(messagesBefore.size() > 0)
		jmsService.send(queue: 'queue',sentMessage)

		Thread.sleep(2000)

		assertNotNull(ListeningService.lastMessageReceived!=null)
		assertTrue(ListeningService.lastMessageReceived.contains("Dodge Stratus"))

	}
	
	//C-1 With Quartz
	@Test
	void testInitiateQuartzJMSJob() {

		listingCompletePostMessageJob = grailsApplication.mainContext.getBean('auctionhaus.ListingCompletePostMessageJob')

		Customer seller = new Customer(username:'steve@email.com',password:"thispass")
		seller.save(flush:true)

		Customer winner = new Customer(username:'bob@email.com',password:"thispas2")
		winner.save(flush:true)

		Listing listing = new Listing(name:"Dodge Intrepid", endDateTime: new Date()-1, isFinished:true, startingPrice: 10, seller: seller, winner:winner)
		listing.save(flush:true)

		//before
		def messagesBefore = jmsService.browse('queue')
		assertFalse(messagesBefore.size() > 0)

		assertNotNull(listingCompletePostMessageJob.getTriggers())
		listingCompletePostMessageJob.execute()

		Thread.sleep(2000);  //need to sleep to allow message to come back over queue
		assertNotNull(ListeningService.lastMessageReceived!=null)
		assertTrue(ListeningService.lastMessageReceived.contains("Dodge Intrepid"))

	}


	//C-2: When a listing completes, send a confirmation email to the winner and the lister with a link to the listing information
	//and the final price. (integration test for extra credit) - Using Green Mail
	@Test
	void testSendGreenMail() {
		Map mail = [message:'hello world', from:'admin@auctionhaus.com', to:'user@gmail.com', subject:'subject']

		mailService.sendMail {
			to mail.to
			from mail.from
			subject mail.subject
			body mail.message
		}

		assertEquals(1, greenMail.getReceivedMessages().length)

		def message = greenMail.getReceivedMessages()[0]

		assertEquals(mail.message, GreenMailUtil.getBody(message))
		assertEquals(mail.from, GreenMailUtil.getAddressList(message.from))
		assertEquals(mail.subject, message.subject)
	}

	//C-2: When a listing completes, send a confirmation email to the winner and the lister with a link to the listing information
	//and the final price. (integration test for extra credit) - Using mail plugin
	@Test
	void testSendMail() {

		Customer seller = new Customer(username:'steve@email.com',password:"thispass")
		seller.save(flush:true)

		Customer winner = new Customer(username:'bob@email.com',password:"thispas2")
		winner.save(flush:true)

		Listing listing = new Listing(name:"Mustang", endDateTime: new Date()+10, isFinished:true, startingPrice: 10, seller: seller, winner:winner)
		listing.save(flush:true)

		List messages = completedListingService.getMessages(listing)
		assertNotNull(messages)
		assertTrue(messages.size() == 2)
		def success = emailService.sendEmails(messages)
		assertEquals(success,true)

		assertEquals(2, greenMail.getReceivedMessages().length)

		assertTrue(greenMail.getReceivedMessages()[0].getContent().contains("Mustang"))
		assertTrue(greenMail.getReceivedMessages()[1].getContent().contains("Mustang"))

	}
	
	//C-2 With Quartz
	@Test
	void testInitiateQuartzEmailJob() {

		listingCompleteReminderJob = grailsApplication.mainContext.getBean('auctionhaus.ListingCompleteReminderJob')

		Customer seller = new Customer(username:'steve@email.com',password:"thispass")
		seller.save(flush:true)

		Customer winner = new Customer(username:'bob@email.com',password:"thispas2")
		winner.save(flush:true)

		Listing listing = new Listing(name:"Bulldozer", isFinished:true, endDateTime: new Date()-1, startingPrice: 10, seller: seller, winner:winner)
		listing.save(flush:true)


		assertNotNull(listingCompleteReminderJob.getTriggers())
		listingCompleteReminderJob.execute()

		assertEquals(2, greenMail.getReceivedMessages().length)

		assertTrue(greenMail.getReceivedMessages()[0].getContent().contains("Bulldozer"))
		assertTrue(greenMail.getReceivedMessages()[1].getContent().contains("Bulldozer"))

	}
}
